---
title: "Groovy 闭包"
date: 2019-9-30
categories:
- groovy
tags:
---

<div id="content">
<p>
Jenkins Pipeline 的语法
</p>
<blockquote>
<p>
<a href="https://cloud.tencent.com/developer/article/1460199">https://cloud.tencent.com/developer/article/1460199</a>
</p>
</blockquote>
<div class="org-src-container">
<pre class="src src-groovy"><span style="font-weight: bold; font-style: italic;">// </span><span style="font-weight: bold; font-style: italic;">定义闭包</span>
<span style="font-weight: bold;">def</span> <span style="font-weight: bold; font-style: italic;">codeBlack</span> = {print <span style="font-style: italic;">"hello closure"</span>}

<span style="font-weight: bold; font-style: italic;">// </span><span style="font-weight: bold; font-style: italic;">闭包当做函数调用</span>
codeBlack

<span style="font-weight: bold; font-style: italic;">// </span><span style="font-weight: bold; font-style: italic;">闭包可以赋值给变量，或者作为参数传递</span>
<span style="font-weight: bold;">def</span> <span style="font-weight: bold;">pipeline</span>(closure) {
  closure()
}
pipeline(codeBlack)

<span style="font-weight: bold; font-style: italic;">// </span><span style="font-weight: bold; font-style: italic;">因为括号是非必需的，下面几种写法结果是一样的，是不是和Jenkins pipeline很像呢</span>

pipeline( {print <span style="font-style: italic;">"hello closure"</span>} )
pipeline { 
 <span style="font-weight: bold;"> print</span> <span style="font-style: italic;">"hello closure"</span>
} 
pipeline codeBlack

<span style="font-weight: bold;">def</span> <span style="font-weight: bold;">stage</span>(<span style="font-weight: bold; text-decoration: underline;">String</span> <span style="font-weight: bold; font-style: italic;">name</span>, closure) {
 <span style="font-weight: bold;"> println</span> name
  closure()
}

<span style="font-weight: bold; font-style: italic;">// </span><span style="font-weight: bold; font-style: italic;">正在情况下，我们这样使用stage函数</span>

stage(<span style="font-style: italic;">"stage name"</span>, {
  <span style="font-weight: bold;"> println</span> <span style="font-style: italic;">"closure"</span>
})

<span style="font-weight: bold; font-style: italic;">// </span><span style="font-weight: bold; font-style: italic;">最终打印</span>
<span style="font-weight: bold; font-style: italic;">/*</span>
<span style="font-weight: bold; font-style: italic;">stage name</span>
<span style="font-weight: bold; font-style: italic;">closure</span>
<span style="font-weight: bold; font-style: italic;">*/</span>
<span style="font-weight: bold; font-style: italic;">// </span><span style="font-weight: bold; font-style: italic;">但是，在Groovy里，可以直接这么写</span>

stage(<span style="font-style: italic;">"stage name"</span>) {
 <span style="font-weight: bold;"> print</span> <span style="font-style: italic;">"closure"</span>
}


</pre>
</div>
</div>
<div class="status" id="postamble">
<p class="date">Date: 2019-9-30</p>
<p class="author">Author: gdme1320</p>
<p class="validation"><a href="http://validator.w3.org/check?uri=referer">Validate</a></p>
</div>
